package org.hospital.dao.impl;

import java.util.List;

import org.hospital.dao.IPatientDAO;
import org.hospital.pojo.TPatient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class TPatientDAOImpl extends HibernateDaoSupport implements IPatientDAO {
	private static final Logger log = LoggerFactory.getLogger(TUserDAOImpl.class);
	// property constants
	public static final String NAME = "name";
	public static final String ID_NO = "idNo";
	
	public void save(TPatient patient) {
		log.debug("saving TPatient instance");
		try {
			getHibernateTemplate().save(patient);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void delete(TPatient persistentInstance) {
		log.debug("deleting TPatient instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TPatient instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from TPatient as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByIdNo(Object idNo) {
		return findByProperty(ID_NO, idNo);
	}
	
	public void attachDirty(TPatient instance) {
		log.debug("attaching dirty TPatient instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	public TPatient merge(TPatient detachedInstance) {
		log.debug("merging TPatient instance");
		try {
			TPatient result = (TPatient) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
}
